home *** CD-ROM | disk | FTP | other *** search
- /* $Id: util.c,v 1.1.1.1 1996/06/07 01:00:45 sverrehu Exp $ */
- /**************************************************************************
- *
- * FILE util.c
- * MODULE OF psorder - move negative pages in a .ps-file
- *
- * DESCRIPTION Misc. functions that may come handy in other programs
- * later. :-)
- *
- * WRITTEN BY Sverre H. Huseby <sverrehu@ifi.uio.no>
- *
- **************************************************************************/
-
- #include <stdio.h>
- #include <unistd.h>
-
- #include <shhmsg.h>
-
- #include "util.h"
-
- /**************************************************************************
- * *
- * P U B L I C F U N C T I O N S *
- * *
- **************************************************************************/
-
- /*-------------------------------------------------------------------------
- *
- * NAME fileOpenReadSeekable
- *
- * FUNCTION Open a file for reading, possibly stdin.
- *
- * SYNOPSIS FILE *fileOpenReadSeekable(const char *filename);
- *
- * INPUT filename
- * name of file to open, or NULL to use stdin.
- *
- * RETURNS An open file, aborts in case of error.
- *
- * DESCRIPTION If filename indicates stdin (ie. it equals NULL), a
- * temporary file is created. Anything coming from
- * stdin is put in this file, untill EOF is
- * reached. When that happens, the temporary file is
- * rewinded, and returned. The temporary file will
- * automatically be removed when closed, or when the
- * program exits normally.
- *
- */
- FILE *fileOpenReadSeekable(const char *filename)
- {
- FILE *ret;
- int c;
-
- if (filename) {
- /* open the given file */
- msgVerbose(2, "opening %s for reading\n", filename);
- if ((ret = fopen(filename, "r")) == NULL)
- msgFatalPerror(filename);
- } else {
- /* read from stdin to a temporary file */
- msgVerbose(2, "reading from stdin to a temporary file\n");
- if ((ret = tmpfile()) == NULL)
- msgFatalPerror("temporary file");
- while ((c = getchar()) != EOF)
- if (putc(c, ret) == EOF)
- msgFatalPerror("temporary file");
- rewind(ret);
- }
- return ret;
- }
-
-
-
- /*-------------------------------------------------------------------------
- *
- * NAME fileOpenWrite
- *
- * FUNCTION Open a file for writing, possibly stdout.
- *
- * SYNOPSIS FILE *fileOpenWrite(const char *filename, int force);
- *
- * INPUT filename
- * name of file to open, or NULL to use stdout.
- * force force deletion of existing file? if not,
- * abort the program if the file exists.
- *
- * RETURNS An open file, aborts in case of error.
- *
- * DESCRIPTION If filename indicates stdout (ie. it equals NULL),
- * nothing special is done.
- *
- * NOTE The opened file is neither seekable, nor readable.
- *
- */
- FILE *fileOpenWrite(const char *filename, int force)
- {
- FILE *ret;
-
- if (filename) {
- /* open the given file */
- if (!force && access(filename, 0) == 0)
- msgFatal("%s: file exists\n", filename);
- msgVerbose(2, "opening %s for writing\n", filename);
- if ((ret = fopen(filename, "w")) == NULL)
- msgFatalPerror(filename);
- } else {
- /* write to stdout */
- msgVerbose(2, "writing to stdout\n");
- ret = stdout;
- }
- return ret;
- }
-